<?php

namespace app\crm\controller;

use app\crm\model\ReceiveTask as ReceiveModel;
use app\crm\model\User as UserModel;


class User extends BaseController
{
    /**
     * 用户列表
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function getList($start ='',$end ='',$nickname ='',$level = '',$is_promoter ='',$mobile = ''){
        $param['query'] = [];
        $where = [];
        if (!empty($start)){
            $start = strtotime($start);
            $where['add_time'][] = ['>=',$start];
            $param['query']['start'] = $start;
        }
        if (!empty($end)){
            $end = strtotime($end);
            $where['add_time'][] = ['<=',$end];
            $param['query']['end'] = $end;
        }
        if (!empty($nickname)){
            $where['nickname'] = $nickname;
            $param['query']['nickname'] = $nickname;
        }
        if (!empty($level) || $level === '0'){
            $where['level'] = $level;
            $param['query']['level'] = $level;
        }
        if (!empty($is_promoter)){
            $where['is_promoter'] = $is_promoter;
            $param['query']['is_promoter'] = $is_promoter;
        }
        if (!empty($mobile)){
            $where['mobile'] = $mobile;
            $param['query']['mobile'] = $mobile;
        }
        $list = UserModel::where($where)->with('user')->order('add_time decs')->paginate(15,false,$param);
        $page = $list->render();
        $count = UserModel::where($where)->with('user')->count('id');
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('count',$count);
        return $this->fetch();
    }

    /**
     * 粉丝列表
     * @param $user_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function fansList($user_id){
        $list = UserModel::where('parentid',$user_id)->with('user')->order('add_time desc')->paginate(10);
        $page = $list->render();
        $this->assign('list',$list);
        $this->assign('page',$page);
        return $this->fetch();
    }



    //重置密码
    public function resetPwd(){
        $user_id = input('post.user_id');
        $user = UserModel::get($user_id);
        if(!$user){
            return json(['errorCode'=>1,'msg'=>'用户ID有误']);
        }
        $password = encpwd('111111',$user['salt']);
        $user->password = $password;
        $res = $user->save();
        if ($res === false){
            return json(['errorCode'=>1,'msg'=>'密码重置失败']);
        }
        return json(['errorCode'=>0,'msg'=>'密码重置成功']);
    }

    public function test(){
        dump(input('get.'));
    }


    //修改支付宝账号
    public function updateAlipay($user_id){
        $info = UserModel::field('id,nickname,alipay,realname')->find($user_id);
        $this->assign('info',$info);
        return $this->fetch();
    }

    //修改支付宝账号--处理
    public function updateAlipayHandle(){
        $param = input('post.');
        $data['realname'] = $param['realname'];
        $data['alipay'] = $param['alipay'];
        $data['id'] = $param['id'];
        $info = UserModel::get(['alipay'=>$data['alipay'],'id'=>['<>',$data['id']]]);
        if ($info){
            return json(['errorCode'=>1,'msg'=>'修改失败,该支付宝已绑定其他账号']);
        }
        $res = UserModel::update($data);
        if ($res ===false){
            return json(['errorCode'=>1,'msg'=>'支付宝修改失败']);
        }
        return json(['errorCode'=>0,'msg'=>'支付宝修改成功']);
    }

    //用户领取任务列表
    public function receiveList($user_id){
        $list = ReceiveModel::where('user_id',$user_id)->with('task_user,task')->order('add_time desc')->paginate(15);
        $page = $list->render();
        $this->assign('list',$list);
        $this->assign('page',$page);
        return $this->fetch();
    }

}
